package org.jeecg.modules.baidu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.baidu.utils.BDMapUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
@Api(tags = "百度地图服务")
@RestController
@RequestMapping("/baidu/map")
public class BDMapController {

    /**
     * 计算驾车距离和时间 - 请求参数类
     */
    @Data
    public static class DrivingRequest {
        private String origin;
        private String destination;
    }

    /**
     * 逆地理编码 - 请求参数类
     */
    @Data
    public static class ReverseGeocodingRequest {
        private String lat;        // 纬度
        private String lng;        // 经度
        private String coordtype;  // 坐标类型，可选：wgs84ll(默认), gcj02ll, bd09ll
    }

    /**
     * 计算驾车距离和时间
     *
     * @param request 起点和终点坐标
     * @return
     */
    @AutoLog(value = "百度地图-计算驾车距离")
    @PostMapping("/driving")
    @ApiOperation(value = "计算驾车距离和时间", notes = "根据起点和终点坐标计算驾车距离和时间")
    public Result<Map<String, Object>> calculateDrivingDistance(@RequestBody DrivingRequest request) {
        try {
            Map<String, String> params = new LinkedHashMap<>();
            params.put("origin", request.getOrigin());
            params.put("destination", request.getDestination());
            params.put("output", "json");

            Map<String, Object> result = BDMapUtil.getDirectionDrivingV2(params);
            if (result != null) {
                return Result.OK("计算成功", result);
            } else {
                return Result.error("计算失败");
            }
        } catch (Exception e) {
            log.error("计算驾车距离异常", e);
            return Result.error("计算异常：" + e.getMessage());
        }
    }

    /**
     * 逆地理编码
     * 根据经纬度坐标获取地址信息
     *
     * @param request 经纬度坐标
     * @return
     */
    @AutoLog(value = "百度地图-逆地理编码")
    @PostMapping("/reverse-geocoding")
    @ApiOperation(value = "逆地理编码", notes = "根据经纬度坐标获取地址信息")
    public Result<Map<String, Object>> getAddressByLocation(@RequestBody ReverseGeocodingRequest request) {
        try {
            if (request.getLat() == null || request.getLat().trim().isEmpty()) {
                return Result.error("纬度不能为空");
            }
            if (request.getLng() == null || request.getLng().trim().isEmpty()) {
                return Result.error("经度不能为空");
            }

            Map<String, Object> result = BDMapUtil.getAddressByLocation(request.getLat(), request.getLng(), request.getCoordtype());
            if (result != null) {
                String status = (String) result.get("status");
                if ("0".equals(status)) {
                    return Result.OK("查询成功", result);
                } else {
                    return Result.error("查询失败：" + result.get("message"));
                }
            } else {
                return Result.error("查询失败");
            }
        } catch (Exception e) {
            log.error("逆地理编码异常", e);
            return Result.error("查询异常：" + e.getMessage());
        }
    }

    /**
     * 逆地理编码（简化版本）
     * 根据经纬度坐标获取格式化地址
     *
     * @param request 经纬度坐标
     * @return
     */
    @AutoLog(value = "百度地图-获取格式化地址")
    @PostMapping("/formatted-address")
    @ApiOperation(value = "获取格式化地址", notes = "根据经纬度坐标获取格式化地址字符串")
    public Result<String> getFormattedAddress(@RequestBody ReverseGeocodingRequest request) {
        try {
            if (request.getLat() == null || request.getLat().trim().isEmpty()) {
                return Result.error("纬度不能为空");
            }
            if (request.getLng() == null || request.getLng().trim().isEmpty()) {
                return Result.error("经度不能为空");
            }

            String address = BDMapUtil.getFormattedAddress(request.getLat(), request.getLng());
            if (address != null && !address.startsWith("请求失败")) {
                return Result.OK("查询成功", address);
            } else {
                return Result.error(address);
            }
        } catch (Exception e) {
            log.error("获取格式化地址异常", e);
            return Result.error("查询异常：" + e.getMessage());
        }
    }
}